<?php  
namespace app\admin\controller;
// use think\Controller;
use think\Image;
class Aboutus extends Base{
	public function aboutus_banner_list(){
		$arr=db('aboutus')->where('type=0')->paginate(5); 
		return view('',['arr'=>$arr]);
	}
	public function aboutus_banner_add(){
		return view();
	}
	public function aboutus_banner_edit(){
		$old=db('aboutus')->find($_GET['id']);
		return view('',['old'=>$old]);
	}
	public function aboutus_banner_update(){
		// halt($_FILES['img']);
		$arr=db('aboutus')->find($_POST['id']);
		if($_FILES['banner']['error']>0){
			$_POST['banner']=$arr['banner'];
		}else{
			unlink(ROOT_PATH.'public'.'/static/uploads/'.$arr['banner']);
			unlink(ROOT_PATH.'public'.'/static/uploads/thumb'.$arr['banner_thumb']);
			$file=request()->file('banner');
			$info=$file->move(ROOT_PATH.'public'.'/static/uploads');
			if($info){
				$banner=$info->getSaveName();
				$banner_filename=$info->getFileName();
			}else{
				echo $info->getError();	
			}
			$banner_thumb = Image::open(ROOT_PATH . 'public' . DS . 'static/uploads/'.$banner);
			$banner_thumb->thumb(150, 150)->save(ROOT_PATH . 'public' . DS . 'static/uploads/thumb/'.$banner_filename);
			$_POST['banner']=$banner;
			$_POST['banner_thumb']=$banner_thumb;
		}
		// halt($_POST['img']);
		if(db('aboutus')->update($_POST)){
			$this->success('修改成功','aboutus_banner_list');
		}else{
			$this->error('修改失败','aboutus_banner_list');
		}
	}
	public function aboutus_banner_insert(){
		// var_dump($_FILES['file']);
		$file=request()->file('banner');
		// var_dump($file);
		$info=$file->move(ROOT_PATH.'public'.'/static/uploads');
		if($info){
			$banner=$info->getSaveName();
			$banner_filename=$info->getFileName();
		}else{
			echo $info->getError();	
		}
		$banner_thumb = Image::open(ROOT_PATH . 'public' . DS . 'static/uploads/'.$banner);
		if(!file_exists(ROOT_PATH . 'public' . DS . 'static/uploads/thumb')){
			mkdir(ROOT_PATH . 'public' . DS . 'static/uploads/thumb');
		}
		$banner_thumb->thumb(150, 150)->save(ROOT_PATH . 'public' . DS . 'static/uploads/thumb/'.$banner_filename);
		$rst=db('aboutus')->insert(['type'=>'0','banner'=>$banner,'banner_thumb'=>$banner_filename]);
		if($rst){
			$this->success('添加成功','aboutus_banner_list');
		}else{
			$this->error('添加失败','aboutus_banner_list');
		}
	}
	public function aboutus_banner_del($id){
		$arr=db('aboutus')->find($id);
		if(db('aboutus')->delete($id)){
			unlink(ROOT_PATH.'public'.'/static/uploads/'.$arr['banner']);
			unlink(ROOT_PATH.'public'.'/static/uploads/thumb/'.$arr['banner_thumb']);
			$this->success('删除成功','aboutus_banner_list');
		}else{
			$this->error('删除失败','aboutus_banner_list');
		}
	}


	public function generalsituation_list(){
		$arr=db('aboutus')->where('type=1')->order('`order` desc')->paginate(5); 
		return view('',['arr'=>$arr]);
	}
	public function generalsituation_add(){
		return view();
	}
	public function generalsituation_insert(){
		// var_dump($_FILES['file']);
		$file=request()->file('img1');
		// var_dump($file);
		$info=$file->move(ROOT_PATH.'public'.'/static/uploads');
		if($info){
			$_POST['img1']=$info->getSaveName();
		}else{
			echo $info->getError();	
		}
		$_POST['type']=1;
		$rst=db('aboutus')->insert($_POST);
		// exit;
		if($rst){
			$this->success('添加成功','generalsituation_list');
		}else{
			$this->error('添加失败','generalsituation_list');
		}
	}
	public function generalsituation_edit(){
		// halt($_POST);
		if(db('aboutus')->update($_POST)){
			$this->success('修改成功','generalsituation_list');
		}else{
			$this->error('修改失败','generalsituation_list');
		}
	}
	public function generalsituation_del($id){
		$arr=db('aboutus')->find($id);
		if(db('aboutus')->delete($id)){
			unlink(ROOT_PATH.'public'.'/static/uploads/'.$arr['img1']);
			$this->success('删除成功','generalsituation_list');
		}else{
			$this->error('删除失败','generalsituation_list');
		}
	}


	public function greatthings_list(){
		$arr=db('aboutus')->where('type=2')->order('year2 desc')->paginate(5); 
		return view('',['arr'=>$arr]);
	}
	public function greatthings_add(){
		return view();
	}
	public function greatthings_insert(){
		// var_dump($_FILES['file']);
		// var_dump($_POST);
		// exit;
		if($file=request()->file('img2')){
			$info=$file->move(ROOT_PATH.'public'.'/static/uploads');
			if($info){
				$_POST['img2']=$info->getSaveName();
			}else{
				echo $info->getError();	
			}
		}else{
			$_POST['img2']='';
		}
		$_POST['type']=2;
		if(db('aboutus')->insert($_POST)){
			$this->success('添加成功','greatthings_list');
		}else{
			$this->error('添加失败','greatthings_list');
		}
	}
	public function greatthings_edit(){
		// var_dump($_GET);exit;
		$old=db('aboutus')->find($_GET['id']);
		return view('',['old'=>$old]);
	}
	public function greatthings_update(){
		// var_dump($_POST);exit;
		$arr=db('aboutus')->find($_POST['id']);
		if($_FILES['img2']['error']>0){
			$_POST['img2']=$arr['img2'];
		}else{
			unlink(ROOT_PATH.'public'.'/static/uploads/'.$arr['img2']);
			$file=request()->file('img2');
			$info=$file->move(ROOT_PATH.'public'.'/static/uploads');
			if($info){
				$_POST['img2']=$info->getSaveName();
			}else{
				echo $info->getError();	
			}
		}
		if(db('aboutus')->update($_POST)){
			$this->success('修改成功','greatthings_list');
		}else{
			$this->error('修改失败','greatthings_list');
		}
	}
	public function greatthings_del($id){
		$arr=db('aboutus')->find($id);
		if(db('aboutus')->delete($id)){
			if($arr['img2']==''){
				$this->success('删除成功','greatthings_list');
			}elseif(unlink(ROOT_PATH.'public'.'/static/uploads/'.$arr['img2'])){
				$this->success('删除成功','greatthings_list');
			}
		}else{
			$this->error('删除失败','greatthings_list');
		}
	}


	public function leadershipcare_list(){
		$arr=db('aboutus')->where('type=3')->paginate(5);
		return view('',['arr'=>$arr]);
	}
	public function leadershipcare_add(){
		return view();
	}
	public function leadershipcare_insert(){
		$file=request()->file('img3');
		$info=$file->move(ROOT_PATH.'public'.'/static/uploads');
		if($info){
			$_POST['img3']=$info->getSaveName();
		}else{
			echo $info->getError();	
		}
		$_POST['type']=3;
		if(db('aboutus')->insert($_POST)){
			$this->success('添加成功','leadershipcare_list');
		}else{
			$this->error('添加失败','leadershipcare_list');
		}
	}
	public function leadershipcare_del($id){
		$arr=db('aboutus')->find($id);
		if(db('aboutus')->delete($id)){
			unlink(ROOT_PATH.'public'.'/static/uploads/'.$arr['img3']);
			$this->success('删除成功','leadershipcare_list');
		}else{
			$this->error('删除失败','leadershipcare_list');
		}
	}
	public function leadershipcare_edit(){
		$old=db('aboutus')->find($_GET['id']);
		return view('',['old'=>$old]);
	}
	public function leadershipcare_update(){
		$arr=db('aboutus')->find($_POST['id']);
		if($_FILES['img3']['error']>0){
			$_POST['img3']=$arr['img3'];
		}else{
			unlink(ROOT_PATH.'public'.'/static/uploads/'.$arr['img3']);
			$file=request()->file('img3');
			$info=$file->move(ROOT_PATH.'public'.'/static/uploads');
			if($info){
				$_POST['img3']=$info->getSaveName();
			}else{
				echo $info->getError();	
			}
		}
		if(db('aboutus')->update($_POST)){
			$this->success('修改成功','leadershipcare_list');
		}else{
			$this->error('修改失败','leadershipcare_list');
		}
	}


	public function socialresponsibility_list(){
		$arr=db('aboutus')->where('type=4')->paginate(5);
		return view('',['arr'=>$arr]);
	}
	public function socialresponsibility_add(){
		return view();
	}
	public function socialresponsibility_insert(){
		$file=request()->file('img4');
		$info=$file->move(ROOT_PATH.'public'.'/static/uploads');
		if($info){
			$_POST['img4']=$info->getSaveName();
		}else{
			echo $info->getError();	
		}
		$_POST['type']=4;
		if(db('aboutus')->insert($_POST)){
			$this->success('添加成功','socialresponsibility_list');
		}else{
			$this->error('添加失败','socialresponsibility_list');
		}
	}
	public function socialresponsibility_del($id){
		$arr=db('aboutus')->find($id);
		if(db('aboutus')->delete($id)){
			unlink(ROOT_PATH.'public'.'/static/uploads/'.$arr['img4']);
			$this->success('删除成功','socialresponsibility_list');
		}else{
			$this->error('删除失败','socialresponsibility_list');
		}
	}
	public function socialresponsibility_edit(){
		$old=db('aboutus')->find($_GET['id']);
		return view('',['old'=>$old]);
	}
	public function socialresponsibility_update(){
		$arr=db('aboutus')->find($_POST['id']);
		if($_FILES['img4']['error']>0){
			$_POST['img4']=$arr['img4'];
		}else{
			unlink(ROOT_PATH.'public'.'/static/uploads/'.$arr['img4']);
			$file=request()->file('img4');
			$info=$file->move(ROOT_PATH.'public'.'/static/uploads');
			if($info){
				$_POST['img4']=$info->getSaveName();
			}else{
				echo $info->getError();	
			}
		}
		if(db('aboutus')->update($_POST)){
			$this->success('修改成功','socialresponsibility_list');
		}else{
			$this->error('修改失败','socialresponsibility_list');
		}
	}
}



